var httpUtil = require('../../utils/httpUtil.js');
// pages/me/me.js
Page({

  /**
   * 页面的初始数据
   */

  data: {
    hidden: false,
    TryCode: '',
    deviceid: '',

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    //获取当前网络状态
    wx.getNetworkType({
      success: function(res) {
        // 返回网络类型, 有效值：
        // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
        if (res.networkType == 'none' || res.networkType == 'unknown') {
          that.setData({
            outline: true
          })
        } else {
          that.setData({
            outline: false,
          })
          wx.getStorage({
            key: 'deviceid',
            success: function(res) {
              that.setData({
                deviceid: res.data,
              })
            },
          })
        }
      }
    })

    //监听网络状态
    wx.onNetworkStatusChange(function(res) {
      if (res.networkType == 'none' || res.networkType == 'unknown') {
        that.setData({
          outline: true
        })
      } else {
        that.setData({
          outline: false,
        })
        wx.getStorage({
          key: 'deviceid',
          success: function(res) {
            that.setData({
              deviceid: res.data,
            })
          },
        })
      }
    })
  },
  voteTitle: function(e) {
    this.setData({
      TryCode: e.detail.value,
    })
  },

  start: function(e) {
    var that = this;
    if (that.data.TryCode == '') {
      wx.showToast({
        title: '体验码不能为空，请输入体验码',
        icon: 'none',
        duration: 1500,
      })
      return;
    }
    that.setData({
      status1: 1,
    })
    var req = {
      deviceid: '' + that.data.deviceid + '',
      openid: wx.getStorageSync("openId"),
    }
    req = JSON.stringify(req)
    httpUtil.post("/crm/senddevicemsg", req)
      .then(function (res) {
        if (res.data.status == 200) {
          that.setData({
            hidden: true
          })
          that.startSetInter()
        } else {
          wx.showToast({
            title: '' + res.data.err_msg + '',
            icon: 'none',
            duration: 1500
          });
        }
      }).catch(function (res) {
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 1500
        });

      })
  },
  startSetInter: function () {
    var that = this;
    var req = {
      deviceid: '' + that.data.deviceid + '',
    }
    req = JSON.stringify(req)
    httpUtil.post("/crm/GetEmpPrice", req)
      .then(function (res) {
        if (res.data.device.status == 1) {
          that.pay();
        } else {
          that.data.status1++;
          if (that.data.status1 < 20) {
            setTimeout(function () {
              that.startSetInter();
            }, 1500)
          } else {
            that.setData({
              hidden: false,
              status1: 1
            })
            wx.showToast({
              title: '设备正在联网，请5秒后扫码重试。。。',
              icon: 'none',
              duration: 2000
            })
            return;
          }
        }
      }).catch(function (res) {
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 1500
        });
      })
  },
  pay: function() {
    var that = this;
    var req = {
      deviceid: that.data.deviceid,
      TryCode: that.data.TryCode
    }
    httpUtil.post("/crm/devicetest", req)
      .then(function(res) {
        that.setData({
          hidden: false,
        })
        wx.showToast({
          title: '' + res.data.message + '',
          icon: 'none',
          duration: 2000,
        })
      }).catch(function(res) {
        console.log(res);
      });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
})